﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class en_ucControl_ucManagerImage : System.Web.UI.UserControl
{
    TblAccountsDAO userDAO = new TblAccountsDAO(Global.CONNECTION_STRING);
    TblAlbumImagesDAO albumimageDAO = new TblAlbumImagesDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected bool CheckAccountType()
    {
        bool flag = false;

        TblAccounts account = new TblAccounts();
        if (Session[Global.SESSION_LOGGED_USER] != null)
            account = (TblAccounts)Session[Global.SESSION_LOGGED_USER]; 

        TblAccounts userAccount = userDAO.GetUser_Check(account.UsernameAcc);
        if (userAccount != null)
        {
            if (userAccount.TypeAcc == 2)
            {
                DataTable dt = null;

                dt = albumimageDAO.CheckNumberAlbum(Global.ConvertToInt(account.IDAcc.ToString()).ToString());
                int numberAlbum = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    numberAlbum += Convert.ToInt32(dr["numberAlbum"].ToString());
                }
                if (numberAlbum >= 1)
                {
                    flag = false; // khong cho insert them nữa
                }
                else
                {
                    flag = true;
                }
            }
            else if(userAccount.TypeAcc == 4)
            {
                flag = true;
            }
        }
        return flag;
    }

    protected void btnAddmew_Click(object sender, EventArgs e)
    {
        if (Session[Global.SESSION_LOGGED_USER] == null)
        {
            Response.Redirect("~/en/Default.aspx");
        }
        try
        {
            if (CheckAccountType())
            {                
                TblAlbumImages Albumimage = new TblAlbumImages();
                TblAccounts account = new TblAccounts();
                if (Session[Global.SESSION_LOGGED_USER] != null)
                    account = (TblAccounts)Session[Global.SESSION_LOGGED_USER]; 

                Albumimage.AlbumName = Global.ConvertToDate((DateTime.Now).ToString()) + "-" + account.IDAcc; 
                Albumimage.DateCreate = DateTime.Now;
                Albumimage.IDAcc = ((TblAccounts)Session[Global.SESSION_LOGGED_USER]).IDAcc;
                Albumimage.Display = true;

                albumimageDAO.InsUpdTblAlbumImages(Albumimage, 1);
            }
            else
            {
                 ScriptManager.RegisterStartupScript(btnAddmew, btnAddmew.GetType(), "alert", "alert(' You have a ablum.!');", true);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
